package com.mycode.finance.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mycode.finance.common.Msg;
import com.mycode.finance.entity.*;
import com.mycode.finance.entity.app.ApiPaging;
import com.mycode.finance.entity.app.ApiPwd;
import com.mycode.finance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author nbly
 * @version 1.0
 * @description: TODO
 * @date 2025/4/28 13:33
 */
@RestController
@RequestMapping("/api/my")
public class MyController {

    @Autowired
    UserService userService;
    @Autowired
    UserChangeMoneyService userChangeMoneyService;
    @Autowired
    UserPayMoneyService userPayMoneyService;
    @Autowired
    UserFundProductService userFundProductService;
    @Autowired
    UserTermFinancialService userTermFinancialService;
    @Autowired
    LoanService loanService;
    @Autowired
    BankCardService bankCardService;

    /**
     * 我的理财
     */
    @PostMapping("/manage")
    @ResponseBody
    public HashMap<String, Object> toMyFinance(@RequestBody User user) {
        HashMap<String, Object> resultMap = new HashMap<>();
        User loginUser = userService.selectUserByTerms(user.getUsername(), user.getPassword());
        Integer userId = loginUser.getId();
        List<UserChangeMoney> userChangeMoneyList = userChangeMoneyService.selectUserChangeMoneyByUserId(userId);
        resultMap.put("userChangeMoneyList", userChangeMoneyList);
        List<UserPayMoney> userPayMoneyList = userPayMoneyService.selectUserPayMoneyByUserId(userId);
        resultMap.put("userPayMoneyList", userPayMoneyList);
        List<UserTermFinancial> userTermFinancialList = userTermFinancialService
                .selectUserTermFinancialByUserId(userId);
        resultMap.put("userTermFinancialList", userTermFinancialList);
        List<UserFundProduct> userFundProductList = userFundProductService.selectUserFundProductByUserId(userId);
        resultMap.put("userFundProductList", userFundProductList);
        resultMap.put("pageTopBarInfo", "我的理财界面");
        resultMap.put("activeUrl1", "personalActive");
        resultMap.put("activeUrl2", "myFinanceActive");
        return resultMap;
    }

    /**
     * 我的借贷
     */
    @PostMapping("/borrow")
    @ResponseBody
    public HashMap<String, Object> toMyLoan(@RequestBody ApiPaging page) {
        HashMap<String, Object> resultMap = new HashMap<>();
        User user = page.getUser();
        User loginUser = userService.selectUserByTerms(user.getUsername(), user.getPassword());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Loan> list = loanService.selectLoanByLoanId(loginUser.getId());
        PageInfo<Loan> pageInfo = new PageInfo<Loan>(list, 5);
        resultMap.put("myLoansPageInfo", pageInfo);
        resultMap.put("loansList", list);

        resultMap.put("pageTopBarInfo", "我的借贷界面");
        resultMap.put("activeUrl1", "personalActive");
        resultMap.put("activeUrl2", "myLoanActive");
        return resultMap;
    }

    /**
     * 银行卡
     */
    @PostMapping("/bank/card")
    @ResponseBody
    public HashMap<String, Object> toBankCard(@RequestBody User user) {
        HashMap<String, Object> resultMap = new HashMap<>();
        User loginUser = userService.selectUserByTerms(user.getUsername(), user.getPassword());
        List<Bankcard> list = bankCardService.selectBankCardByUserId(loginUser.getId());
        resultMap.put("bankCardList", list);
        resultMap.put("pageTopBarInfo", "银行卡管理界面");
        resultMap.put("activeUrl1", "personalActive");
        resultMap.put("activeUrl2", "bankCardActive");
        return resultMap;
    }

    /**
     * 判断并更新密码（用户）
     */
    @PutMapping("/user/updatePwd")
    @ResponseBody
    public HashMap<String, Object> updatePwd(@RequestBody ApiPwd apiPwd) {
        HashMap<String, Object> resultMap = new HashMap<>();
        User loginUser = userService.selectUserByTerms(apiPwd.getUser().getUsername(), apiPwd.getUser().getPassword());
        String id = loginUser.getId().toString();
        User user = userService.selectUserById(Integer.valueOf(id));
        String oldpwd = apiPwd.getOldpwd();
        String newpwd = apiPwd.getNewpwd();
        User verifyExistUser = userService.selectUserByTerms(user.getUsername(), oldpwd);
        if (verifyExistUser != null) {
            user.setPassword(newpwd);
            // 当前登录用户信息改变时session里面存储的用户信息也应该同时改变
            if (Integer.valueOf(id) == (loginUser.getId())) {
                resultMap.put("loginUser", user);
            }
            userService.updateUser(user);
            return resultMap;
        }
        return resultMap;
    }

    /**
     * 获取个人信息
     */
    @PostMapping("/user/personal")
    @ResponseBody
    public HashMap<String, Object> toProfile(@RequestBody User requestUser) {
        HashMap<String, Object> resultMap = new HashMap<>();
        User loginUser = userService.selectUserByTerms(requestUser.getUsername(), requestUser.getPassword());
        User user = userService.selectUserById(loginUser.getId());
        resultMap.put("user", user);
        resultMap.put("pageTopBarInfo", "个人信息界面");
        return resultMap;
    }


    /**
     * 更新用户信息(用户更改自己信息)
     */
    @PostMapping("/user/updateUserProfile")
    @ResponseBody
    public boolean updateUserProfile(@RequestBody User user) {
        User loginUser = userService.selectUserByTerms(user.getUsername(), user.getPassword());
        user.setId(loginUser.getId());
        Integer result = userService.updateUser(user);
        if (result == 1) {
            return true;
        }
        return false;
    }
}
